package com.campus.marketplace.service.impl;

import com.campus.marketplace.entity.Comment;
import com.campus.marketplace.entity.OperationLog;
import com.campus.marketplace.repository.CommentRepository;
import com.campus.marketplace.repository.OperationLogRepository;
import com.campus.marketplace.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Override
    @Transactional
    public Comment addComment(Comment comment) {
        comment.setLevel(1);
        comment.setStatus("PENDING");
        Comment savedComment = commentRepository.save(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("ADD_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(savedComment.getId());
        log.setOperationContent("添加评论: " + comment.getContent());
        operationLogRepository.save(log);

        return savedComment;
    }

    @Override
    @Transactional
    public Comment replyComment(Long parentId, Comment comment) {
        Comment parentComment = commentRepository.findById(parentId)
                .orElseThrow(() -> new RuntimeException("Parent comment not found"));

        comment.setParent(parentComment);
        comment.setProduct(parentComment.getProduct());
        comment.setLevel(parentComment.getLevel() + 1);
        comment.setStatus("PENDING");

        if (comment.getLevel() > 3) {
            throw new RuntimeException("Maximum comment level reached");
        }

        Comment savedComment = commentRepository.save(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("REPLY_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(savedComment.getId());
        log.setOperationContent("回复评论: " + comment.getContent());
        operationLogRepository.save(log);

        return savedComment;
    }

    @Override
    public List<Comment> getProductComments(Long productId) {
        return commentRepository.findByProductIdAndParentIsNullOrderByCreatedAtDesc(productId);
    }

    @Override
    public List<Comment> getCommentReplies(Long commentId) {
        return commentRepository.findByParentIdOrderByCreatedAtAsc(commentId);
    }

    @Override
    @Transactional
    public Comment updateComment(Long commentId, String content) {
        Comment comment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("Comment not found"));

        comment.setContent(content);
        comment.setStatus("PENDING");

        Comment updatedComment = commentRepository.save(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("UPDATE_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(commentId);
        log.setOperationContent("更新评论: " + content);
        operationLogRepository.save(log);

        return updatedComment;
    }

    @Override
    @Transactional
    public void deleteComment(Long commentId) {
        Comment comment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("Comment not found"));

        commentRepository.delete(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("DELETE_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(commentId);
        log.setOperationContent("删除评论");
        operationLogRepository.save(log);
    }

    @Override
    public Comment getCommentById(Long commentId) {
        return commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("Comment not found"));
    }

    @Override
    public List<Comment> getPendingComments() {
        return commentRepository.findByStatus("PENDING");
    }

    @Override
    @Transactional
    public void approveComment(Long commentId) {
        Comment comment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("Comment not found"));

        comment.setStatus("APPROVED");
        commentRepository.save(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("APPROVE_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(commentId);
        log.setOperationContent("审核通过评论");
        operationLogRepository.save(log);
    }

    @Override
    @Transactional
    public void rejectComment(Long commentId) {
        Comment comment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("Comment not found"));

        comment.setStatus("REJECTED");
        commentRepository.save(comment);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(comment.getUser().getId());
        log.setOperationType("REJECT_COMMENT");
        log.setTargetType("COMMENT");
        log.setTargetId(commentId);
        log.setOperationContent("拒绝评论");
        operationLogRepository.save(log);
    }

    @Override
    public Long getCommentCount(Long productId) {
        return commentRepository.countByProductId(productId);
    }
}